//#include "stdafx.h"

#include "log/log2.h"
#include "log/log_type.h"
//#include "Coverage.h"
#include "utility/os.h"
#include "file/file.h"
#include "utility/misc.h"
//#include "utility/string.h"
#include "utility/console.h"
//#include "utility_stream.h"
#include "stream/ostream_out.h"
#include <iostream>
#include <sstream>
#include "utility/assert.h"
#include <string>
//#include <hash_map>
#include "log/functions.hpp"
#include <boost/bind.hpp>

//#include "utility_new.h"

using namespace std;
using namespace boost;
using namespace boost::logging;

namespace KCore
{
std::string g_strLogFolder;
std::string g_strLogTime;

unsigned long g_log_threadid = 0;

void set_log_threadid()
{
	g_log_threadid  = os_getcurthreadid();
}

unsigned long get_log_threadid()
{
	return g_log_threadid;
}

#define CHECK_THREAD_ID assert_exp(os_getcurthreadid() == g_log_threadid); if (os_getcurthreadid() != g_log_threadid) return ;

void add_appender_cout_color(const char *szLog,int color)
{
	LOG_LOGGING << "append cout " << szLog << " color " << color << " thread " << os_getcurthreadid() <<  std::endl;
	add_appender(szLog,boost::bind(write_to_cout_color,_1,_2,color));
}

void add_appender_cout(const char *szLog)
{
	LOG_LOGGING << "append cout " << szLog << " thread " << os_getcurthreadid() << std::endl;
	add_appender(szLog,write_to_cout);
}

void add_appender_file(const char *szLog,bool overwrite)
{
	LOG_LOGGING << "append file " << szLog << " thread " << os_getcurthreadid() << std::endl;
	std::string _log_path;
	log_getfilename(szLog,_log_path);
	add_appender(szLog, write_to_file(_log_path.c_str(),overwrite) );
}

void add_appender_folder(const char *szLog)
{
	LOG_LOGGING << "append folder " << szLog << std::endl;
	std::string _log_path;
	log_getfilename_infolder(szLog,_log_path);
	add_appender(szLog, write_to_file(_log_path));
}


void init_log(const char *folder )
{
	set_log_threadid();

	//COVER;
	//os_setcurrentfolder();
	GUID_INIT;

	dir::md(folder);

	g_strLogFolder = folder;
	g_strLogTime = help_getcurtimestr_forfile();


//	g_mapLog[LOGNAME_APP_ASSERT] = &app_assert();

	add_appender(LOGNAME_APP_ASSERT,boost::bind(write_to_type,_1,_2,KCore::cout_assert));
	add_appender(LOGNAME_APP_PROFILE,boost::bind(write_to_type,_1,_2,KCore::cout_profile));
	add_appender(LOGNAME_APP_ERROR,boost::bind(write_to_type,_1,_2,KCore::cout_err));
	add_appender(LOGNAME_MYOUT,boost::bind(write_to_type,_1,_2,KCore::cout_out));
	add_appender(LOGNAME_APP_ATTENTION,boost::bind(write_to_type,_1,_2,KCore::cout_att));
	add_appender(LOGNAME_APP_WARNING,boost::bind(write_to_type,_1,_2,KCore::cout_warning));
	add_appender(LOGNAME_APP_REPORT,boost::bind(write_to_type,_1,_2,KCore::cout_report));
	//add_appender_folder(LOGNAME_APP_ASSERT);
	//add_appender_folder(LOGNAME_APP_PROFILE);
	cout_set_type_by_exename(KCore::cout_assert,KCore::red2,true,true,"assert.log");
	cout_set_type_by_exename(KCore::cout_err,KCore::red2,true,true,"err.log");
	cout_set_type_by_exename(KCore::cout_warning,KCore::blue2,true,true,"warning.log");
	cout_set_type_by_exename(KCore::cout_report,KCore::yellow2,false,true,"report.log");
	cout_set_type_by_exename(KCore::cout_att,KCore::green2,true,false,"");
	cout_set_type_by_exename(KCore::cout_profile,KCore::red,false,true,"profile.log");
	
	manipulate_logs("*").add_modifier( prepend_time("$hh:$mm:$ss "), "modifier_time",INT_MAX );
	manipulate_logs(LOGNAME_MYOUT).del_modifier("modifier_time");

	LOG_ASSERT << "LOG_ASSERT" << std::endl;
	LOG_PROFILE << "LOG_PROFILE" << std::endl;
	myerr << "myerr" << std::endl;
	myout << "myout" << std::endl;
	myatt << "myatt" << std::endl;
	LOG_WARNING << "LOG_WARNING" << std::endl;
	LOG_LOGGING << "LOG_LOGGING" << std::endl;
	myreport << "myreport" << std::endl;
	LOG_TEMP << "LOG_TEMP" << std::endl;

	flush_log_cache();
}

void log_getfilename(const char *log,std::string& s,bool underexe )
{
	if (!underexe)
		s = g_strLogFolder + "\\"  + log + ".log";
	else
	{
		string exe = os_getexename();
		s = g_strLogFolder + "\\" + exe + "\\"  + log + ".log";
	}
}

void log_getfilename_infolder(const char *log,std::string& s,bool underexe )
{
	if (!underexe)
		s = g_strLogFolder + "\\" + log + "\\" + log + "_" + g_strLogTime + ".log";
	else
	{
		string exe = os_getexename();
		s = g_strLogFolder + "\\" + exe + "\\" + log + "\\" + log + "_" + g_strLogTime + ".log";
	}
	help_buildpath_forfile(s.c_str());
}

};
